﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using WqLunTan.DBContext;
using WqLunTan.Models.DataBase;

namespace WqLunTan.Areas.Manage.Controllers
{
    /// <summary>
    /// 后台管理基类
    /// </summary>
    [Area(AreaName)]
    public class BaseManageController : Controller
    {
        /// <summary>
        /// 区域名称
        /// </summary>
        public const string AreaName = "Manage";

        /// <summary>
        /// 登录的用户
        /// </summary>
        public WqUser LoginUser { get; set; }

        /// <summary>
        /// mysql数据库上下文
        /// </summary>
        protected MySqlDbContext DbContext { get; set; }

        /// <summary>
        /// 配置上下文
        /// </summary>
        protected IConfiguration Config { get; set; }

        /// <summary>
        /// 获取区域内的 url, 不要手动释放, 由DI容器托管生命周期, scope
        /// </summary>
        /// <param name="template"></param>
        /// <returns></returns>
        protected static string AreaUrl(string template)
        {
            return $"{AreaName}/{template}";
        }

        /// <summary>
        /// 控制器执行之前的初始化操作
        /// </summary>
        /// <param name="context"></param>
        public override void OnActionExecuting(ActionExecutingContext context)
        {
            DbContext = context.HttpContext.RequestServices.GetService<MySqlDbContext>();
            Config = context.HttpContext.RequestServices.GetService<IConfiguration>();
            base.OnActionExecuting(context);
        }

        /// <summary>
        /// 控制器执行完成后的操作
        /// </summary>
        /// <param name="context"></param>
        public override void OnActionExecuted(ActionExecutedContext context)
        {
            ViewData["search"] = context.HttpContext.Request.Query["search"].ToString();
            base.OnActionExecuted(context);
        }

        /// <summary>
        /// 参数错误, 状态码400
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        protected JsonResult JsonParamError(object data)
        {
            return new JsonResult(new { msg = "error", data }) { StatusCode = 400 };
        }

        /// <summary>
        /// 程序出现异常,已添加全局的异常过滤器, 状态码500
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        protected JsonResult JsonExceptionError(object data)
        {
            return new JsonResult(new { msg = "error", data }) { StatusCode = 500 };
        }

        /// <summary>
        /// 请求成功
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        protected JsonResult JsonOkResult(object data)
        {
            return new JsonResult(new { msg = "ok", data }) { StatusCode = 200 };
        }

        /// <summary>
        /// 请求完成, 但是未得到成功的执行结果, 比如登录失败等操作
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        protected JsonResult JsonFailureResult(object data)
        {
            return new JsonResult(new { msg = "failure", data }) { StatusCode = 200 };
        }
    }
}